package com.bc.db.dao;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;

import com.bc.db.DBHelper;
import com.bc.db.GenericDao;
import com.bc.db.entity.Warn;
import static com.bc.db.DBHelper.*;

public class WarnDao extends GenericDao<Warn>{

	
	private WarnDao(Context context) {
		super(context);
	}

	public static WarnDao dao;
	
	public static WarnDao getInstance(Context context) {
		if (dao == null) {
			synchronized (WarnDao.class) {
				if (dao == null) {
					dao = new WarnDao(context);
				}
			}
		}
		return dao;
	}
	
	@Override
	protected String getTable() {
		return WARN_TABLE;
	}

	@Override
	protected String[] getColumns() {
		return new String[]{
				ID_COL,
				BUY_COL,
				MONEY_COL,
				OVER_COL,
				TYPE_COL,
				ENABLE_COL
		};
	}

	@Override
	protected Warn getItem(Cursor cursor) {
		Warn warn = new Warn();
		warn.id = cursor.getInt(0);
		warn.buy = cursor.getInt(1);
		warn.money = cursor.getDouble(2);
		warn.over = cursor.getInt(3);
		warn.type = cursor.getString(4);
		warn.enable = cursor.getInt(5);
		return warn;
	}

	@Override
	protected void setItem(ContentValues values, Warn entity) {
		values.put(ID_COL, entity.id);
		values.put(BUY_COL, entity.buy);
		values.put(MONEY_COL, entity.money);
		values.put(OVER_COL, entity.over);
		values.put(TYPE_COL, entity.type);
		values.put(ENABLE_COL, entity.enable);
	}
	
	
	public void addWarn(Warn warn) {
		DBHelper dbHelper =DBHelper.getInstance();
		SQLiteDatabase db = null;
		try {
			db = dbHelper.getWritableDatabase();
			ContentValues values = new ContentValues();
			setItem(values, warn);
			values.remove(ID_COL);
			db.insert(getTable(), null, values);
		} finally {
			close(null, db);
		}
	}

	public void updateWarnEnable(int id, int enable) {
		DBHelper dbHelper = DBHelper.getInstance();
		SQLiteDatabase db = null;
		try {
			db = dbHelper.getWritableDatabase();
			ContentValues values = new ContentValues();
			values.put(ENABLE_COL, enable);
			db.update(getTable(), values, ID_COL+"=?", new String[]{ id + ""});
		} catch (Exception e) {
		} finally {
			close(null, db);
		}
	}
	@Override
	protected String getOrderBy() {
		return null;
	}
	
}
